/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Simulation.java
 *
 * Created on 5 déc. 2011, 11:05:21
 */
package sgbag1.Controlleur;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


import java.awt.event.MouseEvent;
import sgbag1.Noyau.Aeroport;
import sgbag1.Noyau.Bagage;
import sgbag1.Noyau.Chariot;
import sgbag1.Noyau.Guichet;
import sgbag1.Noyau.Rail;
import sgbag1.Noyau.Tapis;
import sgbag1.Noyau.Toboggan;

import sgbag1.Vues.VueAeroport;

/**
 *
 * @author raptorsun
 */
public class Simulation extends javax.swing.JFrame {

    //constants
    private static final int RUN = 10;
    private static final int PAUSE = 10;
    private static final int STOP = 20;
    private static final int MANUEL = 30;
    private static final int AUTO = 40;
    private static final int POPUPON = 10;
    private static final int POPUPOFF = 20;
    private int pop_status;
    private Object objSelected;
    private VueAeroport vueAeroport;
    private Aeroport aeroport;
    private int mode_sim;
    private SimAnimator simAnimator;

    /** Creates new form Simulation */
    public Simulation() {
        //for test only
        //test only codes
        //vueAeroport = new VueAeroport(200, 200);

        // end of test only codes


        initComponents();

        initPanels();

        initSimulation();

    }

    public Simulation(Aeroport ap, VueAeroport va) {
        pop_status = POPUPOFF;
        aeroport = ap;
        vueAeroport = va;
        initComponents();

        initPanels();
        initSimulation();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelMode = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        btnBag = new javax.swing.JButton();
        cartePanel = new javax.swing.JPanel();
        ChariotPopUp = new javax.swing.JPanel();
        jButton11 = new javax.swing.JButton();
        jButton12 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jSpinner2 = new javax.swing.JSpinner();
        jLabel13 = new javax.swing.JLabel();
        TapisPopUp = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jSpinner1 = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jButton6 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        RailPopUp = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jComboBox5 = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jComboBox6 = new javax.swing.JComboBox();
        jButton9 = new javax.swing.JButton();
        jButton10 = new javax.swing.JButton();
        BagagePopUp = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        pointDepart = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        pointArrive = new javax.swing.JComboBox();
        jButton5 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        panelMode.setBorder(javax.swing.BorderFactory.createTitledBorder("Mode de Simulation"));

        jButton1.setText("Demarrer");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Arreter");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Pause");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jRadioButton1.setText("Automatique");
        jRadioButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jRadioButton1MouseReleased(evt);
            }
        });
        jRadioButton1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jRadioButton1StateChanged(evt);
            }
        });
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        jRadioButton2.setText("Manuelle");
        jRadioButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jRadioButton2MouseReleased(evt);
            }
        });
        jRadioButton2.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jRadioButton2StateChanged(evt);
            }
        });

        btnBag.setText("+Bagage");
        btnBag.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBagActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout panelModeLayout = new org.jdesktop.layout.GroupLayout(panelMode);
        panelMode.setLayout(panelModeLayout);
        panelModeLayout.setHorizontalGroup(
            panelModeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(panelModeLayout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jRadioButton1)
                .add(38, 38, 38)
                .add(jRadioButton2)
                .add(18, 18, 18)
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 109, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnBag))
        );
        panelModeLayout.setVerticalGroup(
            panelModeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
            .add(jRadioButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
            .add(jRadioButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
            .add(jButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(btnBag)
        );

        cartePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Aeroport"));

        ChariotPopUp.setBorder(javax.swing.BorderFactory.createTitledBorder("Chariot"));

        jButton11.setText("Modifier");
        jButton11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton11ActionPerformed(evt);
            }
        });

        jButton12.setText("Annuler");
        jButton12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton12ActionPerformed(evt);
            }
        });

        jLabel5.setText("m/s");

        jLabel13.setText("Vitesse");

        org.jdesktop.layout.GroupLayout ChariotPopUpLayout = new org.jdesktop.layout.GroupLayout(ChariotPopUp);
        ChariotPopUp.setLayout(ChariotPopUpLayout);
        ChariotPopUpLayout.setHorizontalGroup(
            ChariotPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ChariotPopUpLayout.createSequentialGroup()
                .addContainerGap()
                .add(ChariotPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(ChariotPopUpLayout.createSequentialGroup()
                        .add(jLabel13)
                        .add(18, 18, 18)
                        .add(jSpinner2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel5))
                    .add(ChariotPopUpLayout.createSequentialGroup()
                        .add(jButton12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jButton11)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        ChariotPopUpLayout.setVerticalGroup(
            ChariotPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(ChariotPopUpLayout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .add(ChariotPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(ChariotPopUpLayout.createSequentialGroup()
                        .add(ChariotPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel13)
                            .add(jSpinner2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(ChariotPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton12)
                            .add(jButton11)))
                    .add(jLabel5)))
        );

        ChariotPopUpLayout.linkSize(new java.awt.Component[] {jLabel13, jLabel5, jSpinner2}, org.jdesktop.layout.GroupLayout.VERTICAL);

        TapisPopUp.setBorder(javax.swing.BorderFactory.createTitledBorder("Tapis"));

        jLabel3.setText("Vitesse");

        jLabel4.setText("m/s");

        jButton6.setText("Modifier");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton8.setText("Annuler");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout TapisPopUpLayout = new org.jdesktop.layout.GroupLayout(TapisPopUp);
        TapisPopUp.setLayout(TapisPopUpLayout);
        TapisPopUpLayout.setHorizontalGroup(
            TapisPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TapisPopUpLayout.createSequentialGroup()
                .add(TapisPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(TapisPopUpLayout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 55, Short.MAX_VALUE)
                        .add(jLabel4))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, TapisPopUpLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(jButton8)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(jButton6)))
                .addContainerGap())
        );
        TapisPopUpLayout.setVerticalGroup(
            TapisPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(TapisPopUpLayout.createSequentialGroup()
                .add(TapisPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jSpinner1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(TapisPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton6)
                    .add(jButton8)))
        );

        TapisPopUpLayout.linkSize(new java.awt.Component[] {jLabel3, jLabel4, jSpinner1}, org.jdesktop.layout.GroupLayout.VERTICAL);

        RailPopUp.setBorder(javax.swing.BorderFactory.createTitledBorder("Rail"));

        jLabel9.setText("Numéro");

        jTextField3.setText("44335267");

        jLabel10.setText("Précedant");

        jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Guichet A2", "Toboggan 443" }));

        jLabel11.setText("Suivant");

        jComboBox6.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Guichet A2", "Toboggan 443" }));
        jComboBox6.setSelectedIndex(1);

        jButton9.setText("Modifier");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        jButton10.setText("Annuler");
        jButton10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton10ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout RailPopUpLayout = new org.jdesktop.layout.GroupLayout(RailPopUp);
        RailPopUp.setLayout(RailPopUpLayout);
        RailPopUpLayout.setHorizontalGroup(
            RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(RailPopUpLayout.createSequentialGroup()
                .add(41, 41, 41)
                .add(RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, RailPopUpLayout.createSequentialGroup()
                        .add(jButton10)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 82, Short.MAX_VALUE)
                        .add(jButton9, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 110, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, RailPopUpLayout.createSequentialGroup()
                        .add(RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel10)
                            .add(jLabel9)
                            .add(jLabel11))
                        .add(15, 15, 15)
                        .add(RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jComboBox6, 0, 209, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                            .add(jComboBox5, 0, 209, Short.MAX_VALUE))))
                .addContainerGap())
        );
        RailPopUpLayout.setVerticalGroup(
            RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(RailPopUpLayout.createSequentialGroup()
                .add(RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel9)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel10)
                    .add(jComboBox5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboBox6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel11))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(RailPopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton10)
                    .add(jButton9))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        BagagePopUp.setBorder(javax.swing.BorderFactory.createTitledBorder("Bagage"));

        jLabel7.setText("Point de départ");

        jLabel8.setText("Point d'arrivée");

        jButton5.setText("Ajouter");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton7.setText("Annuler");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout BagagePopUpLayout = new org.jdesktop.layout.GroupLayout(BagagePopUp);
        BagagePopUp.setLayout(BagagePopUpLayout);
        BagagePopUpLayout.setHorizontalGroup(
            BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(BagagePopUpLayout.createSequentialGroup()
                .addContainerGap()
                .add(BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, BagagePopUpLayout.createSequentialGroup()
                        .add(jButton7)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 93, Short.MAX_VALUE)
                        .add(jButton5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 122, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, BagagePopUpLayout.createSequentialGroup()
                        .add(BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel7)
                            .add(jLabel8))
                        .add(15, 15, 15)
                        .add(BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(pointArrive, 0, 227, Short.MAX_VALUE)
                            .add(pointDepart, 0, 227, Short.MAX_VALUE))))
                .addContainerGap())
        );

        BagagePopUpLayout.linkSize(new java.awt.Component[] {jButton5, jButton7}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        BagagePopUpLayout.setVerticalGroup(
            BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(BagagePopUpLayout.createSequentialGroup()
                .add(24, 24, 24)
                .add(BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(pointDepart, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(pointArrive, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel8))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(BagagePopUpLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton7)
                    .add(jButton5))
                .addContainerGap())
        );

        org.jdesktop.layout.GroupLayout cartePanelLayout = new org.jdesktop.layout.GroupLayout(cartePanel);
        cartePanel.setLayout(cartePanelLayout);
        cartePanelLayout.setHorizontalGroup(
            cartePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(cartePanelLayout.createSequentialGroup()
                .add(28, 28, 28)
                .add(BagagePopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(37, 37, 37)
                .add(TapisPopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 238, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(244, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, cartePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(ChariotPopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 85, Short.MAX_VALUE)
                .add(RailPopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(195, 195, 195))
        );
        cartePanelLayout.setVerticalGroup(
            cartePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(cartePanelLayout.createSequentialGroup()
                .add(52, 52, 52)
                .add(cartePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(RailPopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 165, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(ChariotPopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cartePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(cartePanelLayout.createSequentialGroup()
                        .add(73, 73, 73)
                        .add(TapisPopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(cartePanelLayout.createSequentialGroup()
                        .add(30, 30, 30)
                        .add(BagagePopUp, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 165, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(62, 62, 62))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Retour a l'accueil"));

        jButton4.setText("Retour");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jButton4)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jButton4)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(panelMode, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 92, Short.MAX_VALUE)
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(cartePanel, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(panelMode, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(cartePanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 502, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // start simulation
        hidePopups();
        if (mode_sim == AUTO) {
            addBagages();
            simAnimator = new SimAnimator(vueAeroport, aeroport, graphicAirport);
            simAnimator.setAuto(true);
            simAnimator.start();
        }

        if (mode_sim == MANUEL) {

            simAnimator = new SimAnimator(vueAeroport, aeroport, graphicAirport);
            simAnimator.setAuto(false);
            simAnimator.start();
        }

        if (mode_sim == PAUSE) {
            simAnimator.Start();
        }

        if (mode_sim == AUTO || mode_sim == PAUSE || mode_sim == MANUEL) {
            jButton1.setEnabled(false);
            jButton2.setEnabled(true);
            jButton3.setEnabled(true);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        Bagage b = new Bagage(aeroport.getListeGuichets().get(pointDepart.getSelectedIndex()).getTapis(), aeroport.getListeToboggans().get(pointArrive.getSelectedIndex()));
        aeroport.nouveauBagage(b);

        BagagePopUp.setVisible(false);
        pop_status = POPUPOFF;
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        new FenetrePrincipale().setVisible(true);
        this.dispose();

    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed

        RailPopUp.setVisible(false);
        pop_status = POPUPOFF;
    }//GEN-LAST:event_jButton9ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed

        BagagePopUp.setVisible(false);
        pop_status = POPUPOFF;
    }//GEN-LAST:event_jButton7ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        Tapis tapis = (Tapis) objSelected;



        tapis.setVitesse(((Integer)jSpinner1.getValue()).doubleValue());

        TapisPopUp.setVisible(false);
        pop_status = POPUPOFF;
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

        TapisPopUp.setVisible(false);
        pop_status = POPUPOFF;

    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed

        RailPopUp.setVisible(false);
        pop_status = POPUPOFF;
    }//GEN-LAST:event_jButton10ActionPerformed

    private void jButton11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton11ActionPerformed
        // TODO add your handling code here:
        Chariot chariot = (Chariot) objSelected;
        chariot.setVitesseActuelle(((Integer) jSpinner2.getValue()).doubleValue());

        ChariotPopUp.setVisible(false);
        pop_status = POPUPOFF;
    }//GEN-LAST:event_jButton11ActionPerformed

    private void jButton12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton12ActionPerformed
        // TODO add your handling code here:
        ChariotPopUp.setVisible(false);
        pop_status = POPUPOFF;
    }//GEN-LAST:event_jButton12ActionPerformed

    private void jRadioButton1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jRadioButton1StateChanged

        if (jRadioButton1.isSelected()) {
            jRadioButton2.setSelected(false);
            mode_sim = AUTO;
            btnBag.setEnabled(false);
            simAnimator.setAuto(true);

        }


    }//GEN-LAST:event_jRadioButton1StateChanged

    private void jRadioButton2StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jRadioButton2StateChanged

        if (jRadioButton2.isSelected()) {
            jRadioButton1.setSelected(false);
            mode_sim = MANUEL;
            btnBag.setEnabled(true);
            simAnimator.setAuto(false);

        }

    }//GEN-LAST:event_jRadioButton2StateChanged

    private void jRadioButton2MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButton2MouseReleased
        msgbox("Veuillez ajouter des bagages avant de lancer simulation");

        // mode manuel action here
        System.out.println(mode_sim);
    }//GEN-LAST:event_jRadioButton2MouseReleased

    private void jRadioButton1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jRadioButton1MouseReleased

        // mode auto action here
        System.out.println(mode_sim);
    }//GEN-LAST:event_jRadioButton1MouseReleased

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Pause Sim
        simAnimator.Pause();
        mode_sim = PAUSE;
        jButton3.setEnabled(false);
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // stop sim
        jButton2.setEnabled(false);
        simAnimator.Stop();
        mode_sim = STOP;
        jRadioButton1.setSelected(false);
        jRadioButton2.setSelected(false);
        jButton1.setEnabled(true);
        jButton3.setEnabled(false);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void btnBagActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBagActionPerformed

        int i = 0;
        String name;
        pointDepart.removeAllItems();
        pointArrive.removeAllItems();

        for (Guichet guichet : aeroport.getListeGuichets()) {
            name = "Guichet " + i;
            pointDepart.addItem(name);
            i++;
        }
        i = 0;
        for (Toboggan tbg : aeroport.getListeToboggans()) {
            name = "Toboggan " + i;
            pointArrive.addItem(name);
            i++;
        }
        pop_status = POPUPON;
        BagagePopUp.setVisible(true);
    }//GEN-LAST:event_btnBagActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        simAnimator.Stop();
        
    }//GEN-LAST:event_formWindowClosing


    //draggable


    public class MyMotionListener implements MouseMotionListener {
    private JPanel movingPanel;
    private Point pt;
    public MyMotionListener (JPanel movingPanel) {
        this.movingPanel = movingPanel;
    }
    @Override
    public void mouseDragged(MouseEvent e) {
        pt = SwingUtilities.convertPoint(movingPanel, e.getX(), e.getY(), movingPanel.getParent());
        movingPanel.setBounds(pt.x, pt.y, movingPanel.getWidth(), movingPanel.getHeight());
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
}





    /*
    /* Drag Bagage Popup-Menu     */
    private class CarteMouseListener extends MouseAdapter {

        @Override
        public void mouseClicked(MouseEvent me) {

            System.out.println("dont click me at my " + me.getX() + ", " + me.getY() + " >_<");

            //objSelected = vueAeroport.IsClicked(me.getX(), me.getY());
            if (pop_status == POPUPOFF) {
                objSelected = vueAeroport.getElementClique(me.getX(), me.getY());



                if (objSelected != null) {
                    System.out.println("class : " + objSelected.getClass().toString());
                    Simulation.this.repaint();
                }

                //todo : popup menus
                int coordinateX = 0, coordinateY = 0;
                Point location = new Point();
                location = me.getPoint();


                if (getMenuPopUp() != null) {

                    if ((location.getX() + getMenuPopUp().getWidth()) > cartePanel.getWidth()) {
                        location.setLocation(location.getX() - getMenuPopUp().getWidth(), location.getY());
                    }

                    if ((location.getY() + getMenuPopUp().getHeight()) > cartePanel.getHeight()) {
                        location.setLocation(location.getX(), location.getY() - getMenuPopUp().getHeight());
                    }

                    if(simAnimator.getStatus()==RUN){
                        simAnimator.Pause();
                        mode_sim = PAUSE;
                        jButton3.setEnabled(false);
                        jButton1.setEnabled(true);
                        jButton2.setEnabled(true);
                    }

                    getMenuPopUp().setLocation(location);
                    getMenuPopUp().setVisible(true);
                }

            }

        }
    }

    private JPanel getMenuPopUp() {
        // test only
        //return ChariotPopUp;

        if (objSelected == null) {
            return null;
        }

       if (objSelected.getClass() == Chariot.class) {
            Chariot chariot = (Chariot) objSelected;
            jSpinner2.setValue(chariot.getVitesseActuelle());
            pop_status = POPUPON;
            return ChariotPopUp;
        }

        if (objSelected.getClass() == Guichet.class) {
            int i = 0;
            String name;
            pointDepart.removeAllItems();
            pointArrive.removeAllItems();

            for (Guichet guichet : aeroport.getListeGuichets()) {
                name = "Guichet " + i;
                pointDepart.addItem(name);
                if(objSelected == guichet){
                    pointDepart.setSelectedIndex(i);
                    
                }
                i++;
            }
            i = 0;
            for (Toboggan tbg : aeroport.getListeToboggans()) {
                name = "Toboggan " + i;
                pointArrive.addItem(name);
                i++;
            }
            pop_status = POPUPON;
            
            
            return BagagePopUp;
        }

        if (objSelected.getClass() == Tapis.class) {
            Tapis tapis = (Tapis) objSelected;
            jSpinner1.setValue(tapis.getVitesse());


            pop_status = POPUPON;
            return TapisPopUp;
        }

        if (objSelected.getClass() == Rail.class) {
            pop_status = POPUPON;
            return RailPopUp;
        }

        //return null;
        
        return null;

    }

    private void hidePopups() {
        BagagePopUp.setVisible(false);

        TapisPopUp.setVisible(false);
        RailPopUp.setVisible(false);
        ChariotPopUp.setVisible(false);



    }

    private void msgbox(String str) {
        javax.swing.JOptionPane.showMessageDialog(null, str);
    }

    public Aeroport setAeroport(Aeroport ap) {
        aeroport = ap;



        simAnimator.addAeroport(ap);
        return aeroport;
    }

    public void addBagages() {
        Bagage b1 = new Bagage(aeroport.getListeGuichets().get(0).getTapis(), aeroport.getListeToboggans().get(0));
        aeroport.nouveauBagage(b1);

        Bagage b2 = new Bagage(aeroport.getListeGuichets().get(1).getTapis(), aeroport.getListeToboggans().get(1));
        aeroport.nouveauBagage(b2);

        Bagage b3 = new Bagage(aeroport.getListeGuichets().get(1).getTapis(), aeroport.getListeToboggans().get(3));
        aeroport.nouveauBagage(b3);

        Bagage b4 = new Bagage(aeroport.getListeGuichets().get(1).getTapis(), aeroport.getListeToboggans().get(1));
        aeroport.nouveauBagage(b4);

        Bagage b5 = new Bagage(aeroport.getListeGuichets().get(0).getTapis(), aeroport.getListeToboggans().get(1));
        aeroport.nouveauBagage(b5);
    }

    public VueAeroport setVueAeroport(VueAeroport va) {
        vueAeroport = va;
        simAnimator.addVueAeroport(va);
        graphicAirport.setAeroport(vueAeroport);
        return vueAeroport;
    }

    private void initPanels() {
        //cartePanel.addMouseListener(new CarteMouseListener());

        //Create vues



        graphicAirport = new GraphicAeroport();//on appel le constructeur sans paramatre pour eviter tout les erreurs
        graphicAirport.setSize(cartePanel.getWidth(), cartePanel.getHeight());

        CarteMouseListener ml = new CarteMouseListener();

        graphicAirport.addMouseListener(ml);
        graphicAirport.setLocation(0, 0);

        //panelMode.setSize(659, 59);

        cartePanel.add(graphicAirport);

        hidePopups();
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
        btnBag.setEnabled(true);


        TapisPopUp.addMouseMotionListener(new MyMotionListener(TapisPopUp));
        BagagePopUp.addMouseMotionListener(new MyMotionListener(BagagePopUp));
        ChariotPopUp.addMouseMotionListener(new MyMotionListener(ChariotPopUp));

        pop_status = POPUPOFF;
    }

    private void initSimulation() {
        mode_sim = STOP;


        simAnimator = new SimAnimator(vueAeroport, aeroport, graphicAirport);


    }
    //window components
    private GraphicAeroport graphicAirport;
    /* End of drag and drop */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel BagagePopUp;
    private javax.swing.JPanel ChariotPopUp;
    private javax.swing.JPanel RailPopUp;
    private javax.swing.JPanel TapisPopUp;
    private javax.swing.JButton btnBag;
    private javax.swing.JPanel cartePanel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton10;
    private javax.swing.JButton jButton11;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JComboBox jComboBox5;
    private javax.swing.JComboBox jComboBox6;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JSpinner jSpinner2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JPanel panelMode;
    private javax.swing.JComboBox pointArrive;
    private javax.swing.JComboBox pointDepart;
    // End of variables declaration//GEN-END:variables
}
